package com.ccc.scala.base7_collection

import scala.collection.mutable.ArrayBuffer


object Collection03_Reduce {
  def main(args: Array[String]): Unit = {
    // 集合 - reduce

    val buffer = ArrayBuffer(1, 2, 3, 4, 5)

    //    val sum = buffer.reduce((x: Int, y: Int) => {
    //      x + y
    //    })
    //简化
    //    val sum = buffer.reduce((x, y) => x + y)
    val sum = buffer.reduce(_ + _)
    println(sum) // 10

    // reduceLeft
    val res1 = buffer.reduceLeft(_ - _)
    println(res1) // -8

    // ReduceRight
    // reversed.reduceLeft[B]((x, y)=> op(y, x))
    // 注意这里是 (y, x)
    val res2 = buffer.reduceRight(_ - _)
    println(res2) // 3
  }

}
